//
//  TeachVC.m
//  UIViewController+VCExtra.m
//
//  Created by jzkj on 16/4/5.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "UIViewController+VCExtra.h"
#import <objc/runtime.h>

@implementation UIViewController (PushExtra)


- (void)navigationPushViewController:(Class)vcClass{
    [UIViewController navigationViewController:self pushViewController:vcClass];
}

- (void)navigationPushViewController:(Class)vcClass animation:(BOOL)animation{
    [UIViewController navigationViewController:self pushViewController:vcClass animation:animation withOperation:nil];
}

- (void)navigationPushViewController:(Class)vcClass withOperation:(void (^)(id vcInstance))operation{
    [UIViewController navigationViewController:self pushViewController:vcClass animation:YES withOperation:operation];
}

- (void)navigationPushViewController:(Class)vcClass animation:(BOOL)animation withOperation:(void (^)(id vcInstance))operation{
    [UIViewController navigationViewController:self pushViewController:vcClass animation:animation withOperation:operation];
}

+ (void)navigationViewController:(UIViewController *)vc pushViewController:(Class)vcClass{
    [UIViewController navigationViewController:vc pushViewController:vcClass animation:YES withOperation:nil];
}

+ (void)navigationViewController:(UIViewController *)vc pushViewController:(Class)vcClass animation:(BOOL)animation withOperation:(void (^)(id vcInstance))operation{
    if([vcClass isSubclassOfClass:[UIViewController class]]){
        if(vc.navigationController){
            __block id pushViewController = [[vcClass alloc] init];
            [pushViewController setHidesBottomBarWhenPushed:YES];
            if(operation){
                operation(pushViewController);
            }
            [vc.navigationController pushViewController:pushViewController animated:animation];
        }else{
            NSLog(@"%@ navigation controller is nil",vc.navigationController);
        }
    }else{
        NSLog(@"%@ object is not a kind of UIViewController",vc);
    }
}
@end


@implementation UIViewController (PopExtral)

- (instancetype)popSelf{
    return [self popSelfWithAnimation:YES];
}

- (NSArray *)popToRootViewController{
    return [self popSelfToRootViewControllerWithAnimation:YES];
}

- (instancetype)poptoViewControllerWithIndex:(NSUInteger)index{
    return [self poptoViewControllerWithIndex:index animation:YES];
}

- (instancetype)popSelfWithAnimation:(BOOL)animation{
    return [self.navigationController popViewControllerAnimated:animation];
}

- (NSArray *)popSelfToRootViewControllerWithAnimation:(BOOL)animation{
    return [self.navigationController popToRootViewControllerAnimated:animation];
}

- (instancetype)poptoViewControllerWithIndex:(NSUInteger)index animation:(BOOL)animation{
    NSArray *viewControllers = self.navigationController.viewControllers;
    if(index < viewControllers.count){
        UIViewController *popVC = viewControllers[index];
        [self.navigationController popToViewController:popVC animated:animation];
        return popVC;
    }
    return nil;
}

@end

@implementation UIViewController (PresentExtral)
- (void)presentViewController:(Class)vcClass{
    [self presentViewController:vcClass animation:YES];
}

- (void)presentViewController:(Class)vcClass withOperation:(void (^)(id presentedVC))operation{
    [self presentViewController:vcClass animation:YES withOperation:operation];
}

- (void)presentViewController:(Class)vcClass animation:(BOOL)animation{
    [self presentViewController:vcClass animation:animation withOperation:nil];
}

- (void)presentViewController:(Class)vcClass animation:(BOOL)animation withOperation:(void(^)(id presentedVC))operation{
    if([vcClass isSubclassOfClass:[UIViewController class]]){
        __block id presentedVC = [[vcClass alloc] init];
        if(operation){
            operation(presentedVC);
        }
        [self presentViewController:presentedVC animated:animation completion:nil];
    }else{
        NSLog(@"%@ class is not a subclass of UIViewController",NSStringFromClass(vcClass));
    }
}

@end

@implementation UIViewController (AppExtral)

+ (id<UIApplicationDelegate> )appDelegate{
    return [[UIApplication sharedApplication] delegate];
}

+ (instancetype)appRootViewController{
    return [[[self appDelegate] window] rootViewController];
}

+ (UINavigationController *)appNavigationController{
    UINavigationController *navVc = (UINavigationController *)[self appRootViewController];
    if([navVc isKindOfClass:[UINavigationController class]]){
        return navVc;
    }else if([navVc isKindOfClass:[UITabBarController class]]){
        id nav = [(UITabBarController *)navVc selectedViewController];
        if([nav isKindOfClass:[UINavigationController class]]){
            return nav;
        }
    }
    return nil;
}

+ (instancetype)appNavigationRootViewController{
    UINavigationController *navVc = [self appNavigationController];
    if(navVc){
        NSArray *viewControllers = [navVc viewControllers];
        return viewControllers.count?viewControllers[0]:nil;
    }
    return nil;
}

+ (instancetype)appNavigationTopViewController{
    UINavigationController *navVc = [self appNavigationController];
    if(navVc){
        return [navVc topViewController];
    }
    return nil;
}

+ (instancetype)appNavigationVisibleViewController{
    UINavigationController *navVc = [self appNavigationController];
    if(navVc){
        return [navVc visibleViewController];
    }
    return nil;
}

+ (UITabBarController *)appTabBarController{
    UITabBarController *tabVC = (UITabBarController *)[self appRootViewController];
    if([tabVC isKindOfClass:[UITabBarController class]]){
        return tabVC;
    }
    return nil;
}

+ (instancetype)appTabBarSelelctedViewController{
    UITabBarController *tabVc = [self appTabBarController];
    if(tabVc){
        return [tabVc selectedViewController];
    }
    return nil;
}

+ (instancetype)appTabBarTopViewController{
    UIViewController *vc = [self appTabBarSelelctedViewController];
    if([vc isKindOfClass:[UINavigationController class]]){
        return [(UINavigationController *)vc topViewController];
    }
    return vc;
}

+ (instancetype)appTabBarVisibleViewController{
    UIViewController *vc = [self appTabBarSelelctedViewController];
    if([vc isKindOfClass:[UINavigationController class]]){
        return [(UINavigationController *)vc visibleViewController];
    }
    return vc;
}

@end

@implementation UIViewController (NavigationItemExtral)

static UIImage *navigationLeftItmeImage = nil;
///block asscoiation key
static const char *__navigationLeftBtnActionKey = "__navigationLeftBtnActionKey";
static const char *__navigationRightBtnActionKey = "__navigationRightBtnActionKey";

- (void)customNavigationBar{
    [self customNavigationBarWithBackgroundImage:nil];
}

- (void)customNavigationBarWithBackgroundImage:(UIImage *)bgImage{
    UINavigationBar *navBar = self.navigationController.navigationBar;
    if(bgImage){
        [navBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    }
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    [navBar setTranslucent:NO];
}

///设置通用的返回图片
- (void)setLeftCustomerItemImage:(UIImage *)image{
    navigationLeftItmeImage = image;
}

+ (void)setLeftCustomerItemImage:(UIImage *)image{
    navigationLeftItmeImage = image;
}

- (UIButton *)createLeftCustomerItemWithTarget:(id)target selector:(SEL)selector{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setFrame:NavigationItemDefualtFrame];
    [leftBtn setTitleColor:NavigationTitleDefualtColor forState:UIControlStateNormal];
    [leftBtn.titleLabel setFont:[UIFont systemFontOfSize:NavigationTitleDefualtFontSize]];
    [leftBtn setReversesTitleShadowWhenHighlighted:YES];
    [leftBtn setBackgroundColor:[UIColor clearColor]];
    [leftBtn setImage:[UIImage imageNamed:@"common_return"] forState:UIControlStateNormal];
    [leftBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    [self.navigationItem setLeftBarButtonItem:leftItem];
    
    return leftBtn;
}

- (UIButton *)setLeftCustomerItemTitle:(NSString *)title{
    UIButton *leftBtn = [self createLeftCustomerItemWithTarget:self selector:@selector(__btnLeftAction)];
    [leftBtn setTitle:title forState:UIControlStateNormal];
    [leftBtn sizeToFit];
    return leftBtn;
}

- (void)setLeftCustomerItem{
//    __weak typeof (self) weakSelf = self;
//    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//        self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
//    }
    
    UIButton *leftBtn = [self createLeftCustomerItemWithTarget:self selector:@selector(__btnLeftAction)];
    if(navigationLeftItmeImage){
        [leftBtn setImage:navigationLeftItmeImage forState:UIControlStateNormal];
        [leftBtn sizeToFit];
    }
}

- (UIButton *)setLeftCustomerItemWithTarget:(id)target selector:(SEL)selector{
    UIButton *leftBtn = [self createLeftCustomerItemWithTarget:target selector:selector];
    return leftBtn;
}

- (UIButton *)setLeftCustomerItemTitle:(NSString *)title target:(id)target selector:(SEL)selector{
    UIButton *leftBtn = [self createLeftCustomerItemWithTarget:target selector:selector];
    
    if(title && title.length){
        [leftBtn setTitle:title forState:UIControlStateNormal];
        [leftBtn sizeToFit];
    }
    
    return leftBtn;
}

- (UIButton *)setLeftCustomerItemImage:(UIImage *)leftImage target:(id)target selector:(SEL)selector{
    UIButton *leftBtn = [self createLeftCustomerItemWithTarget:target selector:selector];
    [leftBtn setImage:leftImage forState:UIControlStateNormal];
    [leftBtn sizeToFit];
    return leftBtn;
}

- (void)__btnLeftAction{
    [self popSelf];
}

- (UIButton *)createRightItemWithTarget:(id)target selector:(SEL)selector{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:NavigationItemDefualtFrame];
    [rightBtn setTitleColor:NavigationTitleDefualtColor forState:UIControlStateNormal];
    [rightBtn.titleLabel setFont:[UIFont systemFontOfSize:NavigationTitleDefualtFontSize]];
    [rightBtn setReversesTitleShadowWhenHighlighted:YES];
    [rightBtn setBackgroundColor:[UIColor clearColor]];
    [rightBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    [self.navigationItem setRightBarButtonItem:rightItem];
    return rightBtn;
}

- (UIButton *)setRightItemImage:(UIImage *)rightImg target:(id)target selector:(SEL)selector{
    UIButton *rightBtn = [self createRightItemWithTarget:target selector:selector];
    if(rightImg){
        [rightBtn setImage:rightImg forState:UIControlStateNormal];
        
        [rightBtn sizeToFit];
    }
    return rightBtn;
}

- (UIButton *)setRightItemTitle:(NSString *)title target:(id)target selector:(SEL)selector{
    UIButton *rightBtn = [self createRightItemWithTarget:target selector:selector];
    
    if(title && title.length){
        [rightBtn setTitle:title forState:UIControlStateNormal];
        [rightBtn sizeToFit];
    }
    
    return rightBtn;
}

///block support  -  block handler more easy to do actions
- (UIButton *)setLeftCustomerItemWithHandler:(void (^)(id sender))clickHandler{
    if(clickHandler){
        objc_setAssociatedObject(self, __navigationLeftBtnActionKey, clickHandler, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    UIButton *leftBtn = [self createLeftCustomerItemWithTarget:self selector:@selector(__leftNavigationItemBtnAction:)];
    if(navigationLeftItmeImage){
        [leftBtn setImage:navigationLeftItmeImage forState:UIControlStateNormal];
        
        [leftBtn sizeToFit];
    }
    
    return leftBtn;
}

- (UIButton *)setLeftCustomerItemTitle:(NSString *)title handler:(void (^)(id sender))clickHandler{
    if(clickHandler){
        objc_setAssociatedObject(self, __navigationLeftBtnActionKey, clickHandler, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    return [self setLeftCustomerItemTitle:title target:self selector:@selector(__leftNavigationItemBtnAction:)];
}

- (UIButton *)setLeftCustomerItemImage:(UIImage *)leftImage handler:(void (^)(id sender))clickHandler{
    if(clickHandler){
        objc_setAssociatedObject(self, __navigationLeftBtnActionKey, clickHandler, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    return [self setLeftCustomerItemImage:leftImage target:self selector:@selector(__leftNavigationItemBtnAction:)];
}

- (UIButton *)setRightItemImage:(UIImage *)rightImg handler:(void (^)(id sender))clickHandler{
    if(clickHandler){
        objc_setAssociatedObject(self, __navigationRightBtnActionKey, clickHandler, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    return [self setRightItemImage:rightImg target:self selector:@selector(__rightNavigationItemBtnAction:)];
}

- (UIButton *)setRightItemTitle:(NSString *)title handler:(void (^)(id sender))clickHandler{
    if(clickHandler){
        objc_setAssociatedObject(self, __navigationRightBtnActionKey, clickHandler, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    return [self setRightItemTitle:title target:self selector:@selector(__rightNavigationItemBtnAction:)];
}

///block private method
- (void)__leftNavigationItemBtnAction:(id)sender{
    void (^clickHandler)(id sender) = objc_getAssociatedObject(self,__navigationLeftBtnActionKey);
    if(clickHandler){
        clickHandler(sender);
    }
}

- (void)__rightNavigationItemBtnAction:(id)sender{
    void (^clickHandler)(id sender) = objc_getAssociatedObject(self,__navigationRightBtnActionKey);
    if(clickHandler){
        clickHandler(sender);
    }
}

@end

@implementation UIViewController (NavigationBarExtral)

+ (void)setupNavigationBar{
    
    UINavigationBar *naviBar = [UINavigationBar appearance];
    naviBar.translucent = NO;//解决导航栏颜色变浅
    [naviBar setBackgroundColor:NavigationBarBgColor];
    if(IsIOSAbove7Version){
        [naviBar setBarTintColor:NavigationBarBgColor];
        [naviBar setTintColor:NavigationTitleDefualtColor];//
    }else{
        [naviBar setTintColor:NavigationBarBgColor];
    }
    [naviBar setTitleTextAttributes:@{NSForegroundColorAttributeName:NavigationTitleDefualtColor}];///改变title颜色
    
    [[UIBarButtonItem appearance] setTintColor:NavigationTitleDefualtColor];///改变barButtonItem字体颜色
}

@end

@implementation UIViewController (NavigationTitleActivityIndicatorCategroy)

- (void)hideTitleActivityIndicator{
    self.navigationItem.titleView = nil;
}
@end
